package com.hisense.proxy.busitrans.controller.input;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hisense.proxy.busitrans.vo.HxFdcprBusitransApplyInvoiceHeadVO;
import com.hisense.proxy.busitrans.vo.input.InvoiceOcrReqVO;
import com.hisense.proxy.service.entity.input_ticket.PurchaseParam;
import com.hisense.proxy.service.entity.input_ticket.StaBookBuildParam;
import com.hisense.proxy.service.entity.output_invoice.InvoiceBuildParam;
import com.hisense.proxy.service.entity.personal_ticket_holder.ReimburseCollection;
import com.hisense.proxy.service.utils.HttpClientUtil;
import com.hisense.proxy.service.utils.URLConfigEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  sscivm-收票和应付单 模块发票相关接口
 * </p>
 *
 * @author tangxqa
 * @since 2022/10/9 08:58
 */
@Slf4j
@RestController
@RequestMapping("/ncc/invoice/input")
public class HxSscivmInputInvoiceController {

    @Resource
    private StaBookBuildParam staBookBuildParam;

    @Resource
    private PurchaseParam purchaseParam;

    @Resource
    private ReimburseCollection reimburseCollection;

    @PostMapping("/ocr")
    public String invoiceOcr(@RequestBody String imageBase64) throws Exception {

        Map<String, Object> paramsMap = staBookBuildParam.buildRecognisePostParam(imageBase64);
        log.info("invoiceOcr {} - {}", paramsMap.get("nsrsbh"), paramsMap.get("orgcode"));
        return HttpClientUtil.jsonPost(URLConfigEnum.RECOGNISE.getUrl(), paramsMap);
    }


    @PostMapping("/uploadpdf")
    public String uploadpdf(@RequestParam String nsrsbh,
                            @RequestParam String orgCode,
                            @RequestBody JSONObject params) throws Exception {

//        JSONObject userInfo = params.getJSONObject("userInfo");
        JSONArray pdfFiles = params.getJSONArray("pdfFiles");

        Map<String, Object> paramsMap = reimburseCollection.uploadpdf(nsrsbh, orgCode, pdfFiles);
        log.info("uploadpdf {} - {}", paramsMap.get("nsrsbh"), paramsMap.get("orgcode"));

        return HttpClientUtil.jsonPost(URLConfigEnum.UPLOADPDF1.getUrl(), paramsMap);

    }


    @PostMapping("/verify")
    public String invoiceVerify(@RequestParam String userName,
                                @RequestParam String nsrsbh,
                                @RequestParam String orgCode,
                                @RequestBody Map<String, Object> invoiceBody) throws Exception {

        Map<String, Object> paramsMap = purchaseParam.PURCHASE_VERIFY(userName,
                nsrsbh,
                orgCode,
                Arrays.asList(invoiceBody));
        log.info("verify: {}", JSON.toJSONString(paramsMap));
        return HttpClientUtil.jsonPost(URLConfigEnum.PURCHASE_VERIFY.getUrl(), paramsMap);
    }

    @PostMapping("/save")
    public String invoiceDirectSave(@RequestParam String userName,
                                    @RequestParam String nsrsbh,
                                    @RequestParam String orgCode,
                                @RequestBody List<Map<String, Object>> invoiceBodys) throws Exception {

        Map<String, Object> paramsMap = purchaseParam.PURCHASE_SAVE(userName, nsrsbh, orgCode, invoiceBodys);
        return HttpClientUtil.jsonPost(URLConfigEnum.PURCHASE_SAVE.getUrl(), paramsMap);
    }

    @PostMapping("/delete")
    public String invoiceDelete(@RequestBody List<Map<String, Object>> invoiceMaps,
                                @RequestParam String orgNsrsbh
                                ) throws Exception {

        Map<String, Object> paramsMap = purchaseParam.purchaseDelete(invoiceMaps, orgNsrsbh);
        log.info("delete: {}", JSON.toJSONString(paramsMap));

        return HttpClientUtil.jsonPost(URLConfigEnum.PURCHASE_DELETE.getUrl(), paramsMap);
    }

    @GetMapping("/collection/query")
    public String queryInvoice(@RequestParam String fphm,
                                @RequestParam String fpdm,
                               @RequestParam String orgNsrsbh) throws Exception {

        Map<String, Object> paramsMap = purchaseParam.query(fphm, fpdm, orgNsrsbh);
        log.info("queryInvoice: {}", JSON.toJSONString(paramsMap));

        return HttpClientUtil.jsonPost(URLConfigEnum.PURCHASE_QUERY.getUrl(), paramsMap);
    }




    public static void main(String[] args) {

//        System.out.println(new SimpleDateFormat("yyyyMMdd").parse("20211122"));
    }





}
